﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Googler.DAL;
using Googler.BLL;
using System.Web.Security;
using HodhodNews.Controllers;
using HodhodNews.Web;
namespace Googler.Web
{
    public partial class Settings : System.Web.UI.Page
    {
        UserController _UserController;
        UserController UserController
        {
            get
            {
                if (_UserController == null)
                {
                    _UserController = new UserController();
                }
                return _UserController;
            }
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            tblSettings.Visible = true;
            txtMinutes.Text = Sessions.AggregationTimerPeriod.ToString();

            if (!IsPostBack)
            {
                RebindKeywords();
            }
        }

        private void PleaseLoginFirst()
        {
            lblMessage.Text = "من فضلك سجل دخولك حتى تتمكن من ضبط الإعدادات";
            lblMessage.Visible = true;
            tblSettings.Visible = false;
        }

        private void RebindKeywords()
        {
            gvKeywords.DataSource = this.UserController.GetKeywords();
            gvKeywords.DataBind();
            var master = this.Master as IHodhodMaster;
            if (master != null)
            {
                master.RefreshKeywords();
            }
        }

        protected void gvKeywords_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "DeleteKeyword")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                this.UserController.RemoveKeywork(id);

                RebindKeywords();
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            var userController = new UserController();
            userController.AddKeyword(txtKeyword.Text);
            this.RebindKeywords();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Sessions.AggregationTimerPeriod = Convert.ToInt32(txtMinutes.Text);
        }
    }
}